﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ARR.Entities;
using System.IO;
using Telerik.Windows.Controls;
using System.Windows.Markup;
using Telerik.Windows.Documents.Model;
using Telerik.Windows.Documents.Layout;
using ARR.UI.ViewModels;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for GeneralCommentsView.xaml
    /// </summary>
    public partial class GeneralCommentsView : UserControl
    {
        public GeneralCommentsView()
        {
            InitializeComponent();

            GeneralCommentsGV.PreparedCellForEdit += new EventHandler<GridViewPreparingCellForEditEventArgs>(GeneralCommentsGV_PreparedCellForEdit);
        }

        void GeneralCommentsGV_PreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
        {
            if (e.Column.UniqueName == "Category")
            {
                GeneralCommentsViewModel model = this.DataContext as GeneralCommentsViewModel;
                currentRadDropDownButton = e.EditingElement as RadDropDownButton;
                RadTreeView treeview = currentRadDropDownButton.DropDownContent as RadTreeView;
                if (treeview != null)
                {
                    GeneralComment item = e.Row.DataContext as GeneralComment;

                    if (item.GCReportType == Entities.ReportType.CategoryReport)
                    {
                        treeview.ItemsSource = model.Categories;
                    }

                    currentItem = e.Row.DataContext;
                }
            }
        }

        private object currentItem;
        private RadDropDownButton currentRadDropDownButton;
        private void RadTreeView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RadTreeView treeview = sender as RadTreeView;
            if (treeview != null && treeview.SelectedItem != null)
            {
                if (currentItem != null)
                {
                    Category cat = ((Category)treeview.SelectedItem);

                    if (cat.Id == 0)
                    {
                        ((GeneralComment)currentItem).Category = null;
                        ((GeneralComment)currentItem).CategoryId = null;
                    }
                    else
                    {
                        ((GeneralComment)currentItem).Category = cat;
                        ((GeneralComment)currentItem).CategoryId = cat.Id;
                    }

                    if (currentRadDropDownButton != null)
                    {
                        currentRadDropDownButton.IsOpen = false;
                        this.GeneralCommentsGV.CommitEdit();
                    }
                }
            }
        }

        //RichTextBox rtb;
        //GeneralComment comment;
        //private void GeneralCommentsGV_RowDetailsVisibilityChanged(object sender, Telerik.Windows.Controls.GridView.GridViewRowDetailsEventArgs e)
        //{
        //    if (e.DetailsElement != null)
        //    {
        //        RadButton btnSave = e.DetailsElement.FindName("btnSaveComment") as RadButton;

        //        rtb = e.DetailsElement.FindName("rtbComment") as RichTextBox;
        //        comment = e.Row.DataContext as GeneralComment;
        //        if (e.Visibility == Visibility.Visible)
        //        {
        //            btnSave.Click += new RoutedEventHandler(btnSave_Click);
        //            if (comment.Comment != null)
        //            {
        //                rtb.Document = (FlowDocument)XamlReader.Parse(HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(comment.Comment, true));
        //            }
        //        }
        //        else
        //        {
        //            btnSave.Click -= new RoutedEventHandler(btnSave_Click);
        //        }
        //    }


        //}

        //void btnSave_Click(object sender, RoutedEventArgs e)
        //{
        //    TextRange tr = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        tr.Save(ms, DataFormats.Xaml);
        //        using (StreamReader reader = new StreamReader(ms))
        //        {
        //            string text = "<FlowDocument>" + System.Text.ASCIIEncoding.Default.GetString(ms.ToArray()) + "</FlowDocument>";
        //            string contentToBeSaved = HTMLConverter.HtmlFromXamlConverter.ConvertXamlToHtml(text);
        //            comment.Comment = contentToBeSaved;
        //        }
        //    }
        //}

    }
}
